package com.anglireports.report.data.studentPage;

import com.alibaba.fastjson.JSONObject;
import com.anglireports.report.bean.photo.PersonalPhoto;
import com.anglireports.report.bean.primary.Course;
import com.anglireports.report.bean.primary.Student;
import com.anglireports.report.bean.report.Concentration;
import com.anglireports.report.bean.report.Emotion;
import com.anglireports.report.bean.report.Reports;
import com.anglireports.report.bean.report.Vitality;
import com.anglireports.report.data.base.ResponseDataModel;
import com.anglireports.report.data.base.ResultCodeEnum;
import com.anglireports.report.data.common.angli.AngliCommonData;
import com.anglireports.report.data.common.zhujian.CommonData;
import com.anglireports.report.response.anliResult.StudentModel;
import com.anglireports.report.response.result.ReportsResult;
import com.anglireports.report.response.result.StudentSnaResult;
import com.anglireports.report.response.util.CommonUtils;
import com.anglireports.report.response.util.DateUtil;

import java.util.*;

/**
 * 获取学生页面数据
 * @author lvjilei
 * @ceateTime 2019/3/28.
 */
public class StudentDataUtil {

    /**
     * 获取学生页面数据
     * @param starttime
     * @param endtime
     * @param course_id
     * @param student_id
     * @return
     */
    public static ResponseDataModel<StudentPageResponse> responseData(String starttime, String endtime,
                                                    String course_id,
                                                    String student_id) {
        ResponseDataModel<StudentPageResponse> studentPageResponse
                = new ResponseDataModel<StudentPageResponse>();
        if(null == starttime || endtime == null ||
                course_id== null || student_id == null){
            studentPageResponse.setErr_code(ResultCodeEnum.PARAM_ERROR.getErr_code());
            studentPageResponse.setErr_msg(ResultCodeEnum.PARAM_ERROR.getErr_msg());
            return studentPageResponse;
        }
        //获取时间段内上课记录
        ReportsResult reportsResult = CommonData.getReports(starttime,
                endtime, course_id, null ,student_id);


        if(reportsResult.getResult_code() == 0){
            Map<String,Reports> reportsMap = megreStudentDataByDate(reportsResult);
            //获取班级
            Course course = CommonData.getCourses(starttime).get(course_id);
            //获取竹间学生信息
            Student student = CommonData.getStudentMap().get(student_id);
            //获取昂立学生接口数据
            Map<String,StudentModel> angliStuMap = AngliCommonData.getStuMap(course.getClass_id());
            //获取竹间学生信息
            StudentModel angliStundentInfo = angliStuMap.get(student.getPhone_number());
            //整合基本信息
            StudentPageResponse pageResponse = setBasticInfo(student, angliStundentInfo, course ,starttime ,endtime);

            //循环每天数据计算平均值
            Iterator<String> iterator = reportsMap.keySet().iterator();
            List<Emotion> emotionsList = new ArrayList<Emotion>();
            List<Concentration> concentrationList = new ArrayList<Concentration>();
            List<Vitality> vitalityList = new ArrayList<Vitality>();

            //实到人数
            Integer shidao = 0;
            //应到人数
            Integer yingdao = 0;

            Set<String> angliSet = angliStuMap.keySet();
            Reports r;
            while(iterator.hasNext()){
                String key = iterator.next();
                r = reportsMap.get(key);
                shidao += Integer.parseInt(r.getStudent_count());
                yingdao += angliSet.size();
                emotionsList.add(r.getEmotion());
                concentrationList.add(r.getConcentration());
                vitalityList.add(r.getVitality());
            }

            Emotion emotionAvg = Emotion.getEmotionAvg(emotionsList);
            Concentration concentrationAvg = Concentration.getConcentraionAvg(concentrationList);
            Vitality vitalityAvg = Vitality.getVitalityAvg(vitalityList);

            //指数一览
            pageResponse = setTargetView(pageResponse, student, emotionAvg, vitalityAvg, concentrationAvg, shidao, yingdao);


            //情绪指标
            List<PieData> pieDatas = new ArrayList<PieData>();
            pieDatas.add(new PieData("平静", emotionAvg.getNormal()));
            pieDatas.add(new PieData("积极", emotionAvg.getPassion()));
            pieDatas.add(new PieData("疑惑", emotionAvg.getQuestion()));
            pieDatas.add(new PieData("负面", emotionAvg.getUpset()));
            pageResponse.setPieData(pieDatas);

            //专注指标
            pageResponse.setZhuanzhu(concentrationAvg.getGuanzhudu());
            //参与指标
            pageResponse.setCanyudu(vitalityAvg.getCanyudu());


            ReportsResult result = CommonData.getReports(starttime,
                    endtime, course_id, null ,null);

            //一段时间内怎么取rank
            pageResponse.setWinnerPics(new String[5]);//暂未定
            pageResponse.setRanking(1);//暂未定

            //个人精彩瞬间
           StudentSnaResult listPics = CommonData.getStudentSnapshots(starttime, endtime,
                   course_id,student_id);

           List<PersonalPhoto> photos = listPics.getStudentSnapshots();
            String[] monmentPics = new String[photos.size()];
            for (int i = 0 ; i < photos.size(); i++){
                monmentPics[i] = photos.get(i).getUrl();
            }
            pageResponse.setMomentPics(monmentPics);
            studentPageResponse.setResponse(pageResponse);
        }
        return studentPageResponse;
    }


    /**
     * 同一学生同一天同一课程数据合并
     * @param reportsResult
     * @return
     */
    public static Map<String,Reports> megreStudentDataByDate(ReportsResult reportsResult){
        Reports re;
        Map<String, Reports> reportsMap = new HashMap<String, Reports>();
        List<Reports> reportsList = reportsResult.getReports();
        for (Reports report : reportsList){
            String reportDate = DateUtil.isoStrToTargetStr(
                    report.getStart_time(), DateUtil.dateFormat2);
            re = reportsMap.get(reportDate);
            if(null != re){
                //同一学生同一天同一课程数据合并
                reportsMap.put(reportDate,re.megreReportsData(report));
            } else {
                reportsMap.put(reportDate, report);
            }
        }
        return reportsMap;
    }

    /**
     * 设置基本信息
     */
    public static StudentPageResponse setBasticInfo(
                                                    Student student,
                                                    StudentModel studentModel,
                                                    Course course,
                                                    String starttime,
                                                    String endtime){
        StudentPageResponse pageResponse = new StudentPageResponse();
        pageResponse.setName(student.getName_ch());
        pageResponse.setDateFrom(DateUtil.isoStrToTargetStr(
                starttime, DateUtil.dateFormat3));
        pageResponse.setDateTo(DateUtil.isoStrToTargetStr(
                endtime, DateUtil.dateFormat3));
        pageResponse.setHeadPic(student.getAvatar_url());
        pageResponse.setSex("男".equals(studentModel.getSex())?"m":"f");
        pageResponse.setClassName(course.getName());
        return  pageResponse;
    }

    /**
     * 指数一览整合
     * @param pageResponse
     * @param student
     * @param emotionAvg
     * @param vitalityAvg
     * @param concentrationAvg
     * @return
     */
    public static StudentPageResponse setTargetView(StudentPageResponse pageResponse, Student student,
                                                 Emotion emotionAvg, Vitality vitalityAvg,
                                                 Concentration concentrationAvg,
                                                    Integer shidao,Integer yingdao){

        RadarData radarData = new RadarData();
        Map<String,Object> avgValue = new HashMap<String, Object>();
        avgValue.put("name", student.getName_ch());
        float[] valueArray = new float[5];
        valueArray[0] = emotionAvg.getActiveEmotionData();
        valueArray[1] = vitalityAvg.getVitalityData();
        valueArray[2] = emotionAvg.getEmotionData();
        valueArray[3] = concentrationAvg.getConcentrationData();
        valueArray[4] = CommonUtils.getNum(((float)shidao/yingdao)*100.0f, "0.0") ;
        avgValue.put("value", valueArray);
        radarData.getData().add(avgValue);
        pageResponse.setRadarData(radarData);
        return pageResponse;
    }



    public static void main(String[] args){
        System.out.println(JSONObject.toJSONString(StudentDataUtil.responseData("2019-03-13T09:47:43.498Z", "2019-03-14T09:47:43.498Z",
                "5c9345c935e1406cbc071939","5c98aca71d563fbc68f6baeb")));
    }
}
